﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection.Emit;

namespace Kokomo.PeInspector.Reflection.Disassembly
{
	/// <summary>
	/// Thrown when <see cref="MsilDecompiler"/> encounters an unknown op code.
	/// </summary>
	public class UnrecognizedOpCodeException : Exception
	{
		private static string BuildMessage(
			OpCode opcode,
			object operand)
		{
			string message = (operand == null)
				? string.Format(Messages.UnrecognizedOpcodeMessagePattern, opcode)
				: string.Format(Messages.UnrecognizedOpcodeOperandMessagePattern, opcode, operand)
				;

			return message;
		}

		/// <summary>
		/// Gets the unknown op code.
		/// </summary>
		public OpCode OpCode { get; private set; }
		/// <summary>
		/// Gets the operand.
		/// </summary>
		public object Operand { get; private set; }

		/// <summary>
		/// Initializes a new instance of <see cref="UnrecognizedOpCodeException"/>
		/// </summary>
		/// <param name="opcode">The unrecognized op code.</param>
		/// <param name="operand">The operand.</param>
		public UnrecognizedOpCodeException(
			OpCode opcode,
			object operand = null
			)
			: base(BuildMessage(opcode, operand))
		{
			this.OpCode = opcode;
			this.Operand = operand;
		}
	}
}
